#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 30, 2010

@author: lxd
'''
from common.web import Web
import re

class TproxyWeb(Web):
    def __init__(self, logfile='', interval_time=180):    
        Web.__init__(self, logfile=logfile, interval_time=interval_time) 
               
    def getMain(self):               
        referer = ''
        url = 'http://www.5uproxy.net/http_fast.html'
        return self.fetchData(referer, url) 

def parseProxy(f):
    '''
    >>> f = open('file/5uproxy.txt')
    >>> p_list = parseProxy(f)
    >>> p_list[0]
    '220.250.22.173:808'
    '''        
    data = f.read()
    reg = r'<td>(?P<ip>[^<>].*?)</td>[\s\S]*?<td width="60">(?P<port>\d+)</td>[\s\S]*?<td width="60">(.*?)</td>'
    p = re.compile(reg, re.IGNORECASE)
    m = p.findall(data) 
    if m:
        return ['%s:%s' % (p[0], p[1]) for p in m if p[2] == 'CN']
    return m  

def getIp(f):
    '''
    >>> f = open('file/ip138.txt')
    >>> getIp(f)
    '[124.64.104.13]'
    '''       
    data = f.read()
    reg = r'\[(([12][0-9][0-9]|[1-9][0-9]|[1-9])\.){3,3}([12][0-9][0-9]|[1-9][0-9]|[1-9])\]' 
    p = re.compile(reg, re.IGNORECASE)
    m = p.search(data)
    if m:
        return m.group()
    return None

    
if __name__ == '__main__':
    import doctest
    doctest.testmod()  
    
#    tproxyWeb = TproxyWeb()
#    f = tproxyWeb.getMain()
  
